package com.example.wan1.base.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.wan1.R;
import com.example.wan1.base.mvp.IBaseView;
import com.example.wan1.widget.LoadingView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dagger.android.support.DaggerDialogFragment;

/***
 * @Author: zxd
 * @CreateDate: 2020/8/26 13:40
 * @Description:
 */
public abstract class SimpleDialogFragment extends DaggerDialogFragment implements IBaseView {

    private LoadingView mLoadingView;
    private LinearLayout loadView;
    private View mRootView;
    private View errorView;
    private ViewGroup inflateView;
    private ViewGroup mContainer;

    //重复加载
    private TextView tvReload;

    protected Context mContext;
    protected Unbinder mUnbinder;

    protected abstract void initData();

    protected abstract void initView();

    protected abstract @LayoutRes
    int initLayoutRes();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
        //dialogFragment全屏
        //setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFullScreen);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.DialogFragmentStyle);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootView = inflater.inflate(initLayoutRes(), container, false);
        mContainer = mRootView.findViewById(R.id.srl);
        //项目页面继承自RootFragment，但是没有SmartRefreshLayout，只能这么弄
        if (mContainer == null) {
            mContainer = mRootView.findViewById(R.id.project_vp);
        }
        if (mContainer == null) {
            mContainer = mRootView.findViewById(R.id.nav_rv);
        }
        if (mContainer == null) {
            mContainer = mRootView.findViewById(R.id.website_fl);
        }

        mContainer.setVisibility(View.GONE);
        inflateView = (ViewGroup) mContainer.getParent();
        mUnbinder = ButterKnife.bind(this, mRootView);
        initView();
        return mRootView;
    }

    public void showLoading() {
        showLoading("玩命加载中...");
    }

    public void showLoading(String msg) {
        View mLoadLayout = LayoutInflater.from(mContext).inflate(R.layout.loading_view, inflateView, true);
        mLoadingView = mLoadLayout.findViewById(R.id.loading_view);
        loadView = mLoadLayout.findViewById(R.id.loading_layout);
        if (mLoadingView != null) {
            mLoadingView.setVisibility(View.VISIBLE);
            TextView shapeTv = mLoadingView.findViewById(R.id.shape_tv);
            shapeTv.setText(msg);
            mLoadingView.startFallAnimator();
        }
    }

    public void hideLoading() {
        if (mLoadingView != null) {
            mLoadingView.setVisibility(View.GONE);
            inflateView.removeView(loadView);
            mContainer.setVisibility(View.VISIBLE);
        }
    }

    public void showError() {
        mLoadingView.setVisibility(View.GONE);
        inflateView.removeView(loadView);
        //如果错误，填充错误的布局
        View mErrorLayout = LayoutInflater.from(mContext).inflate(R.layout.view_error, inflateView, true);
        errorView = mErrorLayout.findViewById(R.id.view_error);
        tvReload = errorView.findViewById(R.id.tv_reload);
        tvReload.setOnClickListener(v -> reload());
    }

    /***
     * 重新加载
     * 相当于下拉刷新
     */
    public void reload() {
        errorView.setVisibility(View.GONE);
        inflateView.removeView(errorView);
    }

    @Override
    public void onDestroy() {
        if (mUnbinder != null)
            mUnbinder.unbind();
        super.onDestroy();
    }

    private boolean isLoaded = false;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isLoaded = false;
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isLoaded && getUserVisibleHint()) {
            initData();
            isLoaded = true;
        }
    }
}
